//  Copyright © 2014 Allen County Public Library.

//        ACPL Mobile is free software: This program is distributed and you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.  ACPL Mobile is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY, including without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with ACPL Mobile. If not, see http://www.gnu.org/licenses/.
package us.in.lib.acpl.Mobile_ACPL;

import java.util.ArrayList;
import java.util.List;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import com.google.gson.Gson;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import us.in.lib.acpl.Mobile_ACPL.models.MyList;
import us.in.lib.acpl.Mobile_ACPL.models.MyListHolder;
import us.in.lib.acpl.Mobile_ACPL.parents.BaseActivity;

@EActivity(R.layout.my_lists_add)
public class MyListsAddActivity extends BaseActivity {
	
	@ViewById
	TextView newListButton;
	
	@ViewById
	ListView listView;
	
	List<MyList> lists;
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
	}
	
	@AfterViews
	public void afterViews(){
		SharedPreferences prefs = this.getSharedPreferences("myPrefs", MODE_PRIVATE);
		String jsonString = prefs.getString("lists", null);
		MyListHolder listHolder;
		if(jsonString==null){
			listHolder = new MyListHolder();
			listHolder.lists = new ArrayList<MyList>();
			lists = listHolder.lists;
		}else{
		    Gson gson = new Gson();
			 
		     listHolder = gson.fromJson(jsonString, MyListHolder.class);
		     
		     lists = listHolder.lists;
		}
		

	     
	     listView.setAdapter(new MyListAdapter());
	     listView.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				
				
			}
	    	 
	     });
	     
	     newListButton.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				
	
				Intent intent = new Intent(v.getContext(), MyListsNewActivity_.class);
				intent.putExtras(getIntent().getExtras());
				intent.putExtra("source", "add");
				startActivity(intent);
				
			}
	    	 
	     });
	}

	
	public class MyListAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return lists.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return lists.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
			convertView = inflater.inflate(R.layout.my_lists_add_item, null);
			MyList list = lists.get(position);
			
			TextView tv = (TextView)convertView.findViewById(R.id.listTitle);
			tv.setText(list.name);
			
			tv = (TextView)convertView.findViewById(R.id.numItems);
			tv.setText(list.listItems.size()+" item(s)");
			
			return convertView;
		}
		
	}
}
